angular.module('myApp', ['infinite-scroll']);
//控制图片大小
var max = document.documentElement.clientWidth;

function initImg(listimg) {
	listimg.parentNode.style.height = (max - 31) / 3.0 + "px";
}
//获取当前系统日期
var time,nowdate;
var myDate = new Date();
var Y = myDate.getFullYear() + ''; //获取完整的年份(4位,1970-????)  
var M = myDate.getMonth() + 1 + ""; //获取当前月份(0-11,0代表1月)  
var D = myDate.getDate() + "";
if (M < 10) {
	M = '0' + M;
}
if (D < 10) {
	D = '0' + D;
}
nowdate = Y + '-' + M + '-' + D;

var listA = document.getElementById('meau').getElementsByTagName('a');
for (var i = 0; i < listA.length; i++) {
	listA[i].onclick = function() {
		for (var i = 0; i < listA.length; i++) {
			listA[i].className = '';
		}
		this.className = "active";
	}
}

function cuxiao($scope, $http) {
	var gid = readData(localStorage.guideId);
	var aid = readData(localStorage.promotionId);
	var ruleType = readData(localStorage.ruleType);
	$scope.Name = readData(localStorage.promotionName);
	//时间段
	var urlTime = getUrl("83findFlashSaleIdAndTime");
	var headerAll = getHeader();
	var params = 'userId=' + gid;
	$http({
		method: 'POST',
		url: urlTime,
		headers: headerAll,
		timeout: 10000,
		//cache: true,
		data: params
	}).success(function(data, status, headers, config) {
		$scope.listTime = [];
		if (data.obj.timeList) {
			var old = data.obj.timeList;
			for (var j = 0; j < old.length; j++) {
				var tm = old[j].enddate; //tm.substr(0,10)
				$scope.listTime.push(old[j]);
				if (j == 0) {
					$scope.listTime[j].cln = 'active'
				}
				if (old[j].enddate.indexOf(nowdate) > -1) {
					$scope.listTime[j].myenddate = tm.substr(10, 6);
				} else {
					$scope.listTime[j].myenddate = tm.substr(0, 10);
				}
			}
			$scope.all_products($scope.listTime[0]);
		} else {
			$scope.listTime = null;
		};
	}).error(function() {
		console.log('时间段错误');
	});
	$scope.products = function(index) { //请求对应时段的商品
			var listA = mui('.active');
			for (var i = 0; i < listA.length; i++) {
				listA[i].classList.remove('active');
			}
			document.getElementById(index.id).className = "active";
			$scope.all_products(index);
		}
		//获取商品
	$scope.all_products = function(index) {
		var ff = index.enddate + '';
		window.clearTimeout(time);
		setTimeout(function() {
			window.clearTimeout(time);
			showTime(ff, 'date', 'hour', 'minete', 'second');
		}, 1000);
		var allgoodsUrl = getUrl("84findProductsByUserIdAndActId");
		var headerAll = getHeader();
		//默认加载第一页，初始值0					
		$scope.pages = 0;
		$scope.totalpage = 1;
		$scope.goodsAll = [];
		$scope.loadMore = function() {
			if ($scope.pages < $scope.totalpage) {
				paramsAll = 'userId=' + gid + '&activityId=' + index.id + '&indexpage=' + $scope.pages + '&pageSize=6';
				$scope.pages = $scope.pages + 1;
				$http({
					method: 'POST',
					url: allgoodsUrl,
					headers: headerAll,
					timeout: 10000,
					//cache: true,
					data: paramsAll
				}).success(function(data, status, headers, config) {
					if (data.obj) {
						var goodsList = data.obj.list;
						for (var i = 0; i < goodsList.length; i++) {
							$scope.goodsAll.push(goodsList[i]);
						}
						for (var i = 0; i < $scope.goodsAll.length; i++) {
							if (!$scope.goodsAll[i].picture) {
								$scope.goodsAll[i].picture = '../../image/shopping/middle.png';
							}
						}
						$scope.totalpage = data.obj.totalpage;
					} else {
						$scope.goodsAll = [];
						$scope.pages = $scope.pages - 1;
					};

				}).error(function() {
					$scope.goodsAll = [];
					$scope.pages = $scope.pages - 1;
					console.log('网络链接异常，请稍后重试(e)');
				});
			}
		}
		$scope.loadMore();
	}
	$scope.detail = function(id) {
		//获取当前商品ID 打开商品详情页面
		localStorage.goodsId = writeData(id);
		mui.openWindow({
			url: 'productDetail.html',
			id: 'productDetail.html'
		});
	}
}